/**
 * @param {string} path
 * @returns {Boolean}
 */
export const isExternal: (path: string) => boolean = (path: string): boolean => {
	return /^(https?:|mailto:|tel:)/.test(path)
}

/**
 * url格式是否合法
 * @param {string} url
 * @returns {Boolean}
 */
export const validateURL: (url: string) => boolean = (url: string): boolean => {
	const reg = /^(https?|ftp):\/\/([a-zA-Z0-9.-]+(:[a-zA-Z0-9.&%$-]+)*@)*((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}|([a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(:[0-9]+)*(\/($|[a-zA-Z0-9.,?'\\+&%$#=~_-]+))*$/
	return reg.test(url)
}

/**
 * 是否全为小写英文字母
 * @param {string} str
 * @returns {Boolean}
 */
export const validateLowerCase: (str: string) => boolean = (str: string): boolean => {
	const reg = /^[a-z]+$/
	return reg.test(str)
}

/**
 * 是否全为大写英文字母
 * @param {string} str
 * @returns {Boolean}
 */
export const validateUpperCase: (str: string) => boolean = (str: string): boolean => {
	const reg = /^[A-Z]+$/
	return reg.test(str)
}

/**
 * 是否全为英文字母
 * @param {string} str
 * @returns {Boolean}
 */
export const validateAlphabets: (str: string) => boolean = (str: string): boolean => {
	const reg = /^[A-Za-z]+$/
	return reg.test(str)
}

/**
 * 邮箱格式是否合法
 * @param {string} email
 * @returns {Boolean}
 */
export const validateEmail: (email: string) => boolean = (email: string): boolean => {
	const reg = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
	return reg.test(email)
}

/**
 * 是否是字符串类型
 * @param {any} value
 * @returns {Boolean}
 */
export const isString: (value: any) => boolean = (value: any): boolean => {
	return typeof value === 'string' || value instanceof String
}

/**
 * 是否是数组类型
 * @param {any} value
 * @returns {Boolean}
 */
export const isArray: (value: any) => boolean = (value: any): boolean => {
	if (typeof Array.isArray === 'undefined') {
		return Object.prototype.toString.call(value) === '[object Array]'
	}
	return Array.isArray(value)
}

/**
 * 是否包含中文
 * @param {string} str
 * @returns {boolean}
 */
export const validateCh: (str: string) => boolean = (str: string): boolean => {
	const reg = /[\u4E00-\u9FA5\uF900-\uFA2D]/
	return reg.test(str)
}

/**
 * 用户名格式是否合法：①可以包英文含字母和数字 ②字母开头 ③长度为2-20
 * @param {string} str
 * @returns {boolean}
 */
export const validateUsername: (str: string) => boolean = (str: string): boolean => {
	const reg = /^[a-zA-Z]\w{1,19}$/
	return reg.test(str)
}
